const nodemailer = require('nodemailer');
const config = require('../config');

// 创建邮件传输器
const transporter = nodemailer.createTransport({
  service: config.email.service,
  auth: {
    user: config.email.user,
    pass: config.email.pass
  }
});

// 发送重置密码邮件
exports.sendResetPasswordEmail = async (toEmail, resetLink) => {
  try {
    const mailOptions = {
      from: config.email.user,
      to: toEmail,
      subject: '重置密码链接',
      html: `
        <p>您请求了重置密码，请点击以下链接：</p>
        <p><a href="${resetLink}">${resetLink}</a></p>
        <p>如果这不是您请求的，请忽略此邮件。</p>
        <p>链接将在1小时后失效。</p>
      `
    };

    await transporter.sendMail(mailOptions);
  } catch (error) {
    console.error('发送邮件错误:', error);
    throw error;
  }
};

// 验证邮件配置
transporter.verify((error) => {
  if (error) {
    console.error('邮件服务配置错误:', error);
  } else {
    console.log('邮件服务已就绪');
  }
});

module.exports = exports;
